<?php

namespace app\api\validate;

use think\Validate;

class ApplyShopValidate extends Validate
{
    protected $rule = [
        'legal_person' => 'require',
        'contact_phone' => 'require|mobile',
        'shop_name' => 'require',
        'province' => 'require',
        'city' => 'require',
        'area' => 'require',
        'business_license' => 'require',
        'logo' => 'require',
        'bg_imgs' => 'require',
        'lat' => 'require',
        'lng' => 'require',
        'store_id' => 'require',

        'img' => 'require',
        'name' => 'require',
        'sell_price' => 'require|isAmount',
        'market_price' => 'require|isAmount',
    ];

    protected $message = [
        'legal_person.require' => '法人不能为空!',
        'contact_phone.require' => '联系电话不能为空!',
        'contact_phone.mobile' => '电话格式有误!',
        'shop_name.require' => '店铺名称不能为空',
        'province.require' => '店铺地址有误',
        'city.require' => '店铺地址有误',
        'area.require' => '店铺地址有误',
        'business_license.require' => '营业执照不能为空',
        'logo.require' => '店铺图标不能为空',
        'bg_imgs.require' => '店铺背景图片不能为空',
        'lat.require' => '获取店铺位置失败',
        'lng.require' => '获取店铺位置失败',
        'store_id.require' => '参数有误',

        'img.require' => '商品图片不能为空',
        'name.require' => '商品名称不能为空',
        'sell_price.require' => '销售价不能为空',
        'market_price.require' => '原价/市场价不能为空',
        'sell_price.isAmount' =>'销售价格式有误',
        'market_price.isAmount' =>'市场价格式有误',
    ];
    protected $scene = [
        'apply' => ['legal_person', 'store_id', 'contact_phone', 'shop_name', 'province', 'city', 'area', 'business_license', 'logo', 'bg_imgs', 'lat', 'lng'],
        'add_goods' => ['store_id', 'img', 'name', 'sell_price', 'market_price'],
    ];

    
    /**
     * 金额校验函数
     * @param $value
     * @param bool $isZero
     * @param bool $negative
     * @return bool
     */
    public function isAmount($value)
    {
        $isZero = false;
        $negative = false;
        // 必须是整数或浮点数，且允许为负
        if (!preg_match("/^[-]?\d+(.\d{1,2})?$/", $value)) {
            return false;
        }
        // 不为 0
        if (!$isZero && empty((int) ($value * 100))) {
            return false;
        }
        // 不为负数
        if (!$negative && (int) ($value * 100) < 0) {
            return false;
        }
        return true;
    }
}
